
#---Portmann & Stojanovic 2017: Electoral discrimination against immigrant-origin candidates---#

#load("PrefVotes")

#-------------------- TABLE 3: CANDIDATE NAME AND ELECTORAL SUCCESS

#---Prepare data

dt3 <- d %>% select(origincode_c8, elected) %>% group_by(origincode_c8, elected) %>%
  summarise (n = n())

dt3 <- gather(dt3, Var, Val, n) %>% 
  unite(VarG, Var, elected) %>% 
  spread(VarG, Val)

dt3$n_0 <- dt3$n_0 + dt3$n_1
dt3 <- dt3 %>% mutate(perc_0 = (n_0/1633)*100, perc_1 = (n_1/288)*100)

dt3$perc_0 <- round(dt3$perc_0, digits = 2)
dt3$perc_1 <- round(dt3$perc_1, digits = 2)

dt3_nonswiss <- dt3[dt3$origincode_c8 > 0,] 
dt3_nonswiss_agg <- colSums(dt3_nonswiss) 
dt3_nonswiss_agg <- as.data.frame(t(dt3_nonswiss_agg))

dt3 <- full_join(dt3, dt3_nonswiss_agg)

o <- c(1:7, 28,0)

dt3$origincode_c8 = factor(dt3$origincode_c8,levels = o, 
                           labels=c("Western European/Nordic/Anglo","Southern European", "Eastern European", 
                                    "Hispanic", "Former Yugoslav & Albanian", "Arabic & Turkish", "Other non-European countries", 
                                    "Total non-Swiss", "Swiss"))
dt3 <- dt3 %>% 
  arrange(origincode_c8)

dt3 <- dt3[,c(1,2,4,3,5)]

dt3$sr <- dt3$n_1/dt3$n_0
dt3$sr <- round(dt3$sr, digits = 2)
  
#---Table

kable((as.matrix(dt3)), caption = "",
      col.names = c("Name origin", "n Candidates", "Candidates (%)", "n elected", "elected (%)", 
                    "Success rate (%)"), format = "pandoc")

